﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using AllInOne.Entity;
using AllInOne.ModelDb;
using AllInOne.Models;
using Newtonsoft.Json;

namespace AllInOne.Common
{
    public static class ConvertToList
    {
        /// <summary>
        /// 将object的List数据转回带类型的List
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public static List<ProductList> ConvertToProductList(object list)
        {
            List<ProductList> returnList = new List<ProductList>();

            foreach (var item in (IEnumerable<dynamic>)list)
            {
                ProductList obj = JsonConvert.DeserializeObject<ProductList>(item.ToString().Replace("{{", "{").Replace("}}", "}"));
                ProductList extract = new ProductList();
                foreach (PropertyInfo prop in obj.GetType().GetProperties())
                {
                    foreach (PropertyInfo prop2 in extract.GetType().GetProperties())
                    {
                        if (prop2.Name == prop.Name)
                        {
                            prop2.SetValue(extract, prop.GetValue(obj));
                        }
                    }
                }
                returnList.Add(extract);
            }

            return returnList;
        }

        public static List<SenderList>ConvertToSenderList(object list)
        {
            List<SenderList> returnList = new List<SenderList>();

            foreach (var item in (IEnumerable<dynamic>)list)
            {
                SenderList obj = JsonConvert.DeserializeObject<SenderList>(item.ToString().Replace("{{", "{").Replace("}}", "}"));
                SenderList extract = new SenderList();
                foreach (PropertyInfo prop in obj.GetType().GetProperties())
                {
                    foreach (PropertyInfo prop2 in extract.GetType().GetProperties())
                    {
                        if (prop2.Name == prop.Name)
                        {
                            prop2.SetValue(extract, prop.GetValue(obj));
                        }
                    }
                }
                returnList.Add(extract);
            }

            return returnList;
        }

        public static List<ReceiverList> ConvertToReceiverList(object list)
        {
            List<ReceiverList> returnList = new List<ReceiverList>();

            foreach (var item in (IEnumerable<dynamic>)list)
            {
                ReceiverList obj = JsonConvert.DeserializeObject<ReceiverList>(item.ToString().Replace("{{", "{").Replace("}}", "}"));
                ReceiverList extract = new ReceiverList();
                foreach (PropertyInfo prop in obj.GetType().GetProperties())
                {
                    foreach (PropertyInfo prop2 in extract.GetType().GetProperties())
                    {
                        if (prop2.Name == prop.Name)
                        {
                            prop2.SetValue(extract, prop.GetValue(obj));
                        }
                    }
                }
                returnList.Add(extract);
            }

            return returnList;
        }

        public static List<CustomBaseData> ConvertToCustomBaseData(object list)
        {
            List<CustomBaseData> returnList = new List<CustomBaseData>();

            foreach (var item in (IEnumerable<dynamic>)list)
            {
                CustomBaseData obj = JsonConvert.DeserializeObject<CustomBaseData>(item.ToString().Replace("{{", "{").Replace("}}", "}"));
                CustomBaseData extract = new CustomBaseData();
                foreach (PropertyInfo prop in obj.GetType().GetProperties())
                {
                    foreach (PropertyInfo prop2 in extract.GetType().GetProperties())
                    {
                        if (prop2.Name == prop.Name)
                        {
                            prop2.SetValue(extract, prop.GetValue(obj));
                        }
                    }
                }
                returnList.Add(extract);
            }

            return returnList;
        }

        public static List<TransporterList> ConvertToTransporterList(object list)
        {
            List<TransporterList> returnList = new List<TransporterList>();

            foreach (var item in (IEnumerable<dynamic>)list)
            {
                TransporterList obj = JsonConvert.DeserializeObject<TransporterList>(item.ToString().Replace("{{", "{").Replace("}}", "}"));
                TransporterList extract = new TransporterList();
                foreach (PropertyInfo prop in obj.GetType().GetProperties())
                {
                    foreach (PropertyInfo prop2 in extract.GetType().GetProperties())
                    {
                        if (prop2.Name == prop.Name)
                        {
                            prop2.SetValue(extract, prop.GetValue(obj));
                        }
                    }
                }
                returnList.Add(extract);
            }

            return returnList;
        }

        /// <summary>
        /// 将object的List数据转回带类型的List
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public static List<BlackList> ConvertToBlackList(object list)
        {
            List<BlackList> returnList = new List<BlackList>();

            foreach (var item in (IEnumerable<dynamic>)list)
            {
                BlackList obj = JsonConvert.DeserializeObject<BlackList>(item.ToString().Replace("{{", "{").Replace("}}", "}"));
                BlackList extract = new BlackList();
                foreach (PropertyInfo prop in obj.GetType().GetProperties())
                {
                    foreach (PropertyInfo prop2 in extract.GetType().GetProperties())
                    {
                        if (prop2.Name == prop.Name)
                        {
                            prop2.SetValue(extract, prop.GetValue(obj));
                        }
                    }
                }
                returnList.Add(extract);
            }

            return returnList;
        }

    }

}
